package com.erp.service.util.net;

import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.protocol.HttpContext;

import android.util.Log;
/**
 * Gzip响应拦截器
 * @author ccl
 *
 */
class GzipResponseInterceptor implements HttpResponseInterceptor {

	private static final String TAG="GzipResponseInterceptor";
	private static final GzipResponseInterceptor instance = new GzipResponseInterceptor();

	/**
	 * 对GZIP内容进行解压
	 */
	public void process(final HttpResponse response, final HttpContext context) throws HttpException, java.io.IOException {

		HttpEntity entity = response.getEntity();
		Header ceheader = entity.getContentEncoding();
		if (ceheader != null) {
			HeaderElement[] codecs = ceheader.getElements();
			for (int i = 0; i < codecs.length; i++) {
				if (codecs[i].getName().equalsIgnoreCase("gzip")) {
					GzipDecompressingEntity inputStream= new GzipDecompressingEntity(response.getEntity());
					long zipLength= inputStream.getContentLength();
					response.setEntity(inputStream);
					Log.v(TAG,"gzip response!content-length:"+zipLength);
					return;
				}
			}
		}
	}

	static public GzipResponseInterceptor getInstance() {
		return instance;
	}
}
